package com.mhc.suzuki.controller;

import com.mhc.haval.security.model.User;
import com.mhc.suzuki.constant.ErrorCodeEnum;
import com.mhc.suzuki.dal.model.GuaranteeDO;
import com.mhc.suzuki.form.GuaranteeAddForm;
import com.mhc.suzuki.service.GuaranteeService;
import com.mhc.suzuki.util.BeanCopierUtil;
import com.mhc.suzuki.util.DateUtil;
import com.mhc.suzuki.vo.GuaranteeDetailVO;
import com.subaru.common.entity.BizResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;

/**
 * 担保管理
 *
 * @author kanon
 * @create 2017-09-22 11:45
 **/
@Slf4j
@RestController
@RequestMapping("/guarantee")
public class GuaranteeController {

    @Autowired
    private GuaranteeService guaranteeService;

    /**
     * 担保管理查询
     *
     * @param customerIdCard
     * @param orderNo
     * @return
     */
    @ApiOperation(value = "担保管理查询", notes = "担保管理查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerIdCard", value = "身份证号码", paramType = "path", required = true, dataType = "String"),
            @ApiImplicitParam(name = "orderNo", value = "订单编号", paramType = "path", required = true, dataType = "String")
    })
    @RequestMapping(value = "/{customerIdCard}/{orderNo}", method = RequestMethod.GET)
    public BizResult<GuaranteeDetailVO> getCustomerInfo(@PathVariable String customerIdCard, @PathVariable String orderNo) {
        if (StringUtils.isNotBlank(customerIdCard) && StringUtils.isNotBlank(orderNo)) {
            GuaranteeDetailVO guaranteeDetailVO = guaranteeService.getGuaranteeDetailInfo(customerIdCard, orderNo);
            return BizResult.create(guaranteeDetailVO, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        }
        return BizResult.create(ErrorCodeEnum.PARAM_EMPTY.getErrCode(), ErrorCodeEnum.PARAM_EMPTY.getErrMsg());
    }

    /**
     * 新增担保记录
     *
     * @param guaranteeAddForm
     * @return
     */
    @ApiOperation(value = "新增担保记录", notes = "新增担保记录")
    @ApiImplicitParam(name = "guaranteeAddForm", value = "担保表单", required = true, paramType = "body", dataType = "GuaranteeAddForm")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public BizResult<Boolean> addGuarantee(@AuthenticationPrincipal User logedUser, @RequestBody GuaranteeAddForm guaranteeAddForm) {
        Optional<User> userOptional = Optional.ofNullable(logedUser);
        if (userOptional.isPresent()) {
            try {
                GuaranteeDO guaranteeDO = BeanCopierUtil.convert(guaranteeAddForm, GuaranteeDO.class);
                guaranteeDO.setPayDate(DateUtil.defaultParseStringToDateTime(guaranteeAddForm.getPayDate()));
                guaranteeDO.setIdCard(guaranteeAddForm.getIdCard());
                return BizResult.create(guaranteeService.addGuarantee(guaranteeDO), ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
            } catch (Exception e) {
                log.error("Exception IN addGuarantee", e);
                return BizResult.create(ErrorCodeEnum.ENTRY_ERROR.getErrCode(), ErrorCodeEnum.ENTRY_ERROR.getErrMsg());
            }
        } else {
            return BizResult.create(ErrorCodeEnum.NOT_LOGIN.getErrCode(), ErrorCodeEnum.NOT_LOGIN.getErrMsg());
        }
    }


}
